package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.UserDTO;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.vos.AdUserVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.lang.annotation.Target;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {



    /**
     * 登录的方法
     * @param userDTO
     * @return
     */
    @Override
    public ResponseResult login(UserDTO userDTO) {
        //获取传递过来的用户名和密码
        String name = userDTO.getName();
        String password = userDTO.getPassword();

        //判断数据
        if (StringUtils.isBlank(name) || StringUtils.isBlank(password)){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE,"用户名或密码不能为空");
        }

        //判断用户名和密码是否正确
        LambdaQueryWrapper<AdUser> wrapper = Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, name);
        AdUser user = this.getOne(wrapper);
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "该用户不存在");
        }

        //判断用户登录的用户的状态
        if (9 != user.getStatus().intValue()){
            CustException.cust(AppHttpCodeEnum.LOGIN_STATUS_ERROR,"用户状态异常,请联系管理员");
        }

        //获取加密后的面
        String password1 = user.getPassword();
        //获取盐
        String salt = user.getSalt();
        //给传递过来的密码加密
        String newPassword = DigestUtils.md5DigestAsHex((password + salt).getBytes(StandardCharsets.UTF_8));
        //判断真正的密码 和输入的密码
        if (!password1.equals(newPassword)){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "密码错误");
        }

        //修改登录时间  修改数据库中的时间
        user.setLoginTime(new Date());

        updateById(user);

        //生成token
        String token = AppJwtUtil.getToken(user.getId().longValue());
        //将用户信息 封装到vo
        AdUserVO adUserVO = new AdUserVO();
        BeanUtils.copyProperties(user,adUserVO);
        //返回结果
        Map<String,Object> map = new HashMap();
        map.put("token",token);
        map.put("user",adUserVO);
        return ResponseResult.okResult(map);
    }
}
